﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization;

namespace ProcrastinationKit.Connectivity.Configuration
{
    
    public class PKConfiguration: System.Configuration.ConfigurationSection
    {
        private static string sConfigurationSectionConst = "ProcrastinationKit";

        public static PKConfiguration GetConfig()
        {
            System.Configuration.Configuration cf = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigFileName);
            System.Configuration.ConfigurationSection ob = cf.GetSection(PKConfiguration.sConfigurationSectionConst);
            PKConfiguration con = new PKConfiguration();
           
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(ob.SectionInformation.GetRawXml());
            XmlNode Pluggins = xDoc.GetElementsByTagName("Pluggins")[0];
            foreach (XmlNode node in Pluggins)
            {
                PKPluggin pk = new PKPluggin();
                pk.Assembly = node.Attributes["assembly"].InnerText;
                pk.Class = node.Attributes["class"].InnerText;
                pk.Name = node.Attributes["name"].InnerText;
                pk.Token = node.Attributes["token"].InnerText;
                con.Pluggins.Add(pk);
            }

           //end of shitty code

            return con;
            
        }

        static private string ConfigFileName
        {
            get
            {
                Assembly me = Assembly.GetExecutingAssembly();
                string fileName = me.ManifestModule.FullyQualifiedName;
                string configFileName = fileName; // +".config";
                return configFileName;
            }
        }

        [System.Configuration.ConfigurationProperty("Settings")]
        public PKSettingsCollection Settings
        {
            get
            {
                return (PKSettingsCollection)this[ "Settings"] ?? new PKSettingsCollection();
            }
        }

        [System.Configuration.ConfigurationProperty("Pluggins")]
        public PKPlugginsCollection Pluggins
        {
            get
            {
                return (PKPlugginsCollection)this["Pluggins"] ?? new PKPlugginsCollection();
            }
        }


       
    }
}
